<template>
  <div class="pre-production-plan-container">
    
    <el-button type="primary">新增</el-button>
    <el-button type="warning">修改</el-button>
    <el-button type="danger">过滤</el-button>
    <el-button type="warning">还原</el-button>
    <el-button type="primary">打印</el-button>
    <el-button type="primary">导入</el-button>
    <el-button type="primary">导出</el-button>
    <el-button type="success">查看日志</el-button>    
    <el-button  type="info">设计</el-button>
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="关联项目">
          <el-input  placeholder="请输入关联项目" clearable />
        </el-form-item>
        <el-form-item label="生产计划状态">
          <el-select  placeholder="全部" style="width:200px;" clearable>
            <el-option label="未生成" value="未生成" />
            <el-option label="生成完毕" value="生成完毕" />
            <el-option label="部分生成" value="部分生成" />
          </el-select>
        </el-form-item>
        <el-form-item label="添加人员">
          <el-input placeholder="请选择添加人员" clearable>
            <template #suffix>
              <el-icon class="el-input__icon" >
                <Search />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="添加日期">
          <el-date-picker
            
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>

        <el-form-item label="预计划编号">
          <el-input  placeholder="请输入预计划编号" clearable />
        </el-form-item>
        <el-form-item label="预计划主题">
          <el-input  placeholder="请输入预计划编号" clearable />
        </el-form-item>
        <el-form-item label="来源单据">
          <el-input  placeholder="请输入预计划编号" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" >查询</el-button>
          <el-button >重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
  <el-card>


   <el-table
        v-loading="loading"
        :data="tableData"
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
        border
        highlight-current-row
      >
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="planCode" label="预生产计划编号" min-width="140" />
        <el-table-column prop="planName" label="预生产计划主题" min-width="150" show-overflow-tooltip />
        <el-table-column prop="sourceType" label="采购单据类型" min-width="120" />
        <el-table-column prop="sourceDocument" label="采购单据" min-width="180" show-overflow-tooltip />
        <el-table-column prop="projectName" label="关联项目" min-width="120" show-overflow-tooltip />
        <el-table-column prop="status" label="预生产计划状态" min-width="120">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="planQuantity" label="本次预生产计划数量" min-width="150" />
        <el-table-column prop="generatedQuantity" label="已生成生产计划数量" min-width="150" />
        <el-table-column prop="generatedQuantity" label="制单人员" min-width="150" />
        <el-table-column prop="generatedQuantity" label="制单日期" min-width="150" />
        <el-table-column label="操作" fixed="right" min-width="180">
          <template #default="{ row }">
            <el-button-group>
              <el-button link type="primary" @click="handleViewDetail(row)">详情</el-button>
              <el-button link type="primary" @click="handleGeneratePlan(row)">生成生产计划</el-button>
            </el-button-group>
          </template>
        </el-table-column>
    </el-table>
  </el-card>
  
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search } from '@element-plus/icons-vue'
import axios from 'axios'
// 表格数据
const loading = ref(false)
const tableData = ref([])
const selectedRows = ref([])
const selectedRow = ref(null)

// 分页数据
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})
// 表单数据
const dialogVisible = ref(false)
const dialogType = ref('add')
const formRef = ref()
const projectLoading = ref(false)
const projectOptions = ref([])

const form = reactive({
  planName: '',
  sourceType: '',
  sourceDocument: '',
  projectName: '',
  planQuantity: 0,
  remark: ''
})

// 搜索表单数据
const searchForm = reactive({
  projectName: '',
  status: '',
  planCode: '',
  planName: '',
  sourceType: '',
  creator: '',
  dateRange: []
})
// 获取表格数据
const getTableData = async () => {
  try {
    loading.value = true
    const params = {
      page: pagination.currentPage,
      pageSize: pagination.pageSize,
      ...searchForm,
      startDate: searchForm.dateRange?.[0],
      endDate: searchForm.dateRange?.[1]
    }
    const response = await axios.get('/api/pre-production/plan/list', { params })
    tableData.value = response.data.records
    pagination.total = response.data.total
  } catch (error) {
    console.error('获取数据失败：', error)
    ElMessage.error('获取数据失败')
  } finally {
    loading.value = false
  }
}
onMounted(() => {
  getTableData()
})


// 搜索项目
const searchProjects = async (query) => {
  if (query.length < 2) return
  projectLoading.value = true
  try {
    const response = await axios.get('/api/project/search', {
      params: { keyword: query }
    })
    projectOptions.value = response.data
  } catch (error) {
    console.error('搜索项目失败：', error)
  } finally {
    projectLoading.value = false
  }
}
// 获取状态类型
const getStatusType = (status) => {
  const statusMap = {
    '未生成': 'warning',
    '生成完毕': 'success',
    '部分生成': 'primary'
  }
  return statusMap[status] || 'info'
}
// 搜索
const handleSearch = () => {
  pagination.currentPage = 1
  getTableData()
}

// 重置搜索
const resetSearch = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  handleSearch()
}
// 表格选择改变
const handleSelectionChange = (rows) => {
  selectedRows.value = rows
  selectedRow.value = rows.length === 1 ? rows[0] : null
}

</script>